﻿using System;
using System.Collections.Generic;
using System.Text;
using SysCommon.Business.CVSS.WS.Query;
using SysCommon.Entity.CVSS.WS.Query;
using SysCommon.Tools;
using System.Windows.Forms;
using dotnetCHARTING.WinForms;
using SysCommon;
using System.Drawing;
namespace CVSS.Client.SC.UI
{
    public class SpeedStakeOutMDT
    {
        private SpeedStakeOut frm;
        private WS_Query_BizCtrlImpl WS_Query;
        private Chart chart;

        public bool ShowForm()
        {
            try
            {
                WS_Query = new WS_Query_BizCtrlImpl();
                WS_Query.GetHistoryByCar_StrCompleted += new SysCommon.Events.CVSS.WS.Query.GetHistoryByCar_StrCompletedEventHandler(WS_Query_GetHistoryByCar_StrCompleted);
                frm=new SpeedStakeOut();
                frm.ButtStat.Click += new EventHandler(ButtStat_Click);

                chart = new Chart();
                SetChartStyle(ref chart);//设置样式

                

                frm.ShowDialog();
                return true;

            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用showForm出错", ex);
                return false;
            }
        }

        void ButtStat_Click(object sender, EventArgs e)
        {

            if (frm.TCarPlate.Text.Trim().Length == 0)
            {
                MessageBox.Show("请输入车牌号码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                frm.TCarPlate.Focus();
                return;
            }

            if (frm.uiDateTime1.StartDateTime > frm.uiDateTime1.EndDateTime)
            {
                return;
            }
            

            if (WS_Query == null) return;
            StringBuilder sqlstr = new StringBuilder();
            sqlstr.AppendFormat(" datedmytime>=to_date('{0}','yyyy-MM-dd hh24:mi:ss') and datedmytime<=to_date('{1}','yyyy-MM-dd hh24:mi:ss')${2}"
                ,frm.uiDateTime1.StartDateTime.ToString()
                ,frm.uiDateTime1.EndDateTime.ToString()
                ,frm.TCarPlate.Text);

             chart.SeriesCollection.Clear();
             SetChartStyle(ref chart);
             chart.RefreshChart();
             
             frm.ButtStat.Enabled = false;

             frm.panelchart.Controls.Remove(chart);

             frm.picwait.Visible = true;
             Application.DoEvents();
             

             WS_Query.GetHistoryByCar_StrAsync(sqlstr.ToString());
            
        }

        void WS_Query_GetHistoryByCar_StrCompleted(object sender, SysCommon.Events.CVSS.WS.Query.GetHistoryByCar_StrCompletedEventArgs e)
        {
            try
            {
                if (e.Result == null || e.Result.Length==0)
                {
                    frm.ButtStat.Enabled = true;
                    frm.picwait.Visible = false;
                    MessageBox.Show("未找到相关数据!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    return; 
                }
                
                LatestGPS_Simple_Ety[] etys = e.Result as LatestGPS_Simple_Ety[];
                

                SeriesCollection SC = new SeriesCollection();
                Series s = new Series();
                s.Name = "速度";
                foreach (LatestGPS_Simple_Ety ety in etys)
                {
                    Element ele = new Element();
                    ele.YValue = ety.SPEED;
                    ele.XDateTime = ety.DATEDMYTIME;
                    s.Elements.Add(ele);
                }
                SC.Add(s);
                
                if (SC == null) return;
                if (chart == null) return;


                chart.Title =frm.TCarPlate.Text + chart.Title;
                chart.SeriesCollection.Add(SC);
                
                frm.panelchart.Controls.Add(chart);

                chart.RefreshChart();

                //Application.DoEvents();

                frm.picwait.Visible = false;
                frm.ButtStat.Enabled = true;

            }
            catch (Exception ex)
            {
                Global.mainLog.Error("调用 WS_Query_GetHistoryByCar_StrCompleted 出错", ex);
                frm.picwait.Visible = false;
                frm.ButtStat.Enabled = true;
            }
            
            
        }

        private void SetChartStyle(ref Chart chart)
        {
            chart.Title = "车速变化图";
            chart.Dock = DockStyle.Fill;
            chart.ChartArea.Background.Color = Color.FromArgb(215, 243, 250);
            chart.ChartArea.XAxis.Label.Text = "日期时间";
            chart.ChartArea.YAxis.Label.Text = "行车速度(公里/小时)";
            chart.LegendBox.Template = "%icon %name";
            chart.TempDirectory = "temp";
            chart.Debug = true;
            chart.ChartArea.XAxis.Scale = dotnetCHARTING.WinForms.Scale.Time;
            chart.ChartArea.XAxis.FormatString = "hh:mm:ss";
            chart.ChartArea.XAxis.TimeInterval = TimeInterval.Hour;
            chart.DefaultSeries.Type = SeriesType.Line;
        }
    }
}
